package com.item.controller;


import com.github.pagehelper.PageInfo;
import com.item.bean.RPage;
import com.item.domain.UserRoleR;
import com.item.po.UserRoleRPo;
import com.item.service.UserRoleRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TUserRoleRController {

    @Autowired
    UserRoleRService userRoleRService;

    @PostMapping(value = "/userRoleR/{limit}/{page}")
    public RPage<UserRoleRPo> UserRoleR(
            @PathVariable(value = "limit", required = false) int limit,
            @PathVariable(value = "page", required = false) int page,
            @RequestBody(required = false) UserRoleR userRoleR) {
        List<UserRoleRPo> userRoleRs = userRoleRService.findAll(limit, page, userRoleR);
        for (UserRoleRPo roleR : userRoleRs) {
            if (roleR.getMenuId() == null) {
                roleR.setMenuId("");
            }
        }
        PageInfo<UserRoleRPo> pageInfo = new PageInfo<>(userRoleRs);
        RPage<UserRoleRPo> rPage = new RPage<>();
        rPage.setList(pageInfo.getList());
        rPage.setPageNum(pageInfo.getPageNum());
        rPage.setTotal(pageInfo.getTotal());
        rPage.setPages(pageInfo.getPages());
        rPage.setPageSize(pageInfo.getPageSize());

        return rPage;
    }

    @PostMapping(value = "/userRoleR/add")
    public Integer userRoleRAdd(@RequestBody UserRoleR userRoleR) {
        userRoleR.setCreateTime(System.currentTimeMillis());
        int i = userRoleRService.add(userRoleR);
        return i;
    }

    @PutMapping(value = "/userRoleR/update")
    public Integer userRoleRUpdate(@RequestBody UserRoleRPo userRoleR) {
        UserRoleR userRoleR1 = new UserRoleR();
        userRoleR1.setId(userRoleR.getId());
        userRoleR1.setUpdateTime(System.currentTimeMillis());
        userRoleR1.setUserId(userRoleR.getId());
        userRoleR1.setUserRoleId(userRoleR.getUserRoleId());
        userRoleR1.setUserName(userRoleR.getUserName());
        String[] menuIds = userRoleR.getMenuIds();
        String menuId = "";
        for (String id : menuIds) {
            menuId += "," + id;
        }
        String substring = menuId.substring(1);
        userRoleR1.setMenuId(substring);
        int i = userRoleRService.update(userRoleR1);
        return i;
    }

    @DeleteMapping(value = "/userRoleR/{id}")
    public Integer userRoleRDelete(@PathVariable(value = "id") int id) {

        int i = userRoleRService.delete(id);
        return i;
    }

}
